import os,sys,time,re,random
import ftplib
from concurrent.futures import ThreadPoolExecutor, wait, ALL_COMPLETED, FIRST_COMPLETED

#host = "ftp.8dbie.com"
#host = "27.151.97.56"
#host = "test.nizz8.com"
#port = 21
host = '100.76.178.69'
port = 9080
username = "zongzheng"
password = "data123"
homeDir = r"/"
localDir = r"\\192.168.10.60\d$\chuanshu\newspaper"


class FTPSync:
    ftp = ftplib.FTP()

    def __init__(self):
        self.ftp.connect(host=host,port=port)
        self.ftp.set_debuglevel(2)  #2为显示详细信息，0关闭
        self.ftp.set_pasv(False)
        self.ftp.encoding = 'GB18030'

    def Login(self, user, passwd):
        self.ftp.login(user, passwd)
        print(self.ftp.welcome)
        '''
        self.ftp.sendcmd('SYST')
        self.ftp.sendcmd('PWD')
        self.ftp.sendcmd('FEAT')
        self.ftp.sendcmd('OPTS UTF8 on')
        self.ftp.sendcmd('MODE Z')
        self.ftp.sendcmd('REST 0')
        self.ftp.sendcmd('PASV')
        self.ftp.sendcmd('LIST')
        self.ftp.getresp()
        '''

    def DownLoadFile(self, LocalFile, RemoteFile):  # 下载当个文件
        file_handler = open(LocalFile, 'wb')
        print(file_handler)
        # self.ftp.retrbinary("RETR %s" % (RemoteFile), file_handler.write)#接收服务器上文件并写入本地文件
        self.ftp.retrbinary('RETR ' + RemoteFile, file_handler.write)
        file_handler.close()
        return True

    def DownLoadFileTree(self, LocalDir, RemoteDir):  # 下载整个目录下的文件
        print("remoteDir:", RemoteDir)
        if not os.path.exists(LocalDir):
            os.makedirs(LocalDir)
        self.ftp.cwd(RemoteDir)
        dir_res = []
        self.ftp.dir('.',dir_res .append)
        print(dir_res)
        dirs = [f.split(None, 8)[-1] for f in dir_res if f.startswith('d')]
        dirs.sort(reverse=True)
        for dir in dirs:
            newlocaldir = os.path.join(LocalDir,dir)
            newlocaldir = newlocaldir.replace('_','-')
            newremotedir = os.path.join(RemoteDir,dir)
            self.ftp.cwd(newremotedir)
            file_res = []
            self.ftp.dir('.',file_res.append)
            for file in file_res:
                if '2019' not in file:
                    continue
                self.downbdb(file,newlocaldir)
            '''
            executor = ThreadPoolExecutor(max_workers=1)
            tasks = []
            for file in file_res:
                if '2019' not in file:
                    continue
                job = executor.submit(self.downbdb,file,newlocaldir)
                tasks.append(job)
            wait(tasks)
            '''
        return

    def downbdb(self,file,newlocaldir):
        try:
            if not os.path.isdir(newlocaldir):
                os.makedirs(newlocaldir)
            g = re.findall(r'(\d{4}-\d{2}-\d{2}.*?bdb)', file)
            filename = g[0]
            serversize = self.ftp.size(filename)
            localfile = os.path.join(newlocaldir, filename)
            localsize = 0
            if os.path.isfile(localfile):
                localsize = os.path.getsize(localfile)
            if localsize != serversize:
                if os.path.isfile(localfile):
                    os.unlink(localfile)
                os.chdir(newlocaldir)
                outf = open(filename, 'wb')
                try:
                    pass
                    print('开始下载:{}，文件大小为：{}MB'.format(filename, int(serversize / 1024 / 1024)))
                    self.ftp.retrbinary('RETR %s' % filename, outf.write)
                    print('结束下载:{}'.format(filename))
                finally:
                    outf.close()
            else:
                pass
                #print('文件已存在：{}'.format(filename))
        except Exception as ex:
            print(str(ex))

    def close(self):
        self.ftp.quit()


if __name__ == '__main__':
    while True:
        try:
            ftp = FTPSync()
            ftp.Login(username,password)
            ftp.DownLoadFileTree(localDir,homeDir)
            ftp.close()
        except Exception as ex:
            print("发生异常:{}".format(str(ex)))
            time.sleep(10)
            continue
        print('{}：5分钟后再次尝试'.format(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())))
        time.sleep(60 * 5)
        print("ok!")
